"""Apply a single random color to the icon and display tint of selected objects."""

from __future__ import annotations

import random

import c4d


def main() -> None:
    doc = c4d.documents.GetActiveDocument()
    if doc is None:
        return

    selection = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN)
    if not selection:
        return

    color = c4d.Vector(*(random.uniform(0.0, 1.0) for _ in range(3)))

    doc.StartUndo()
    try:
        for obj in selection:
            doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)

            obj[c4d.ID_BASELIST_ICON_COLORIZE_MODE] = c4d.ID_BASELIST_ICON_COLORIZE_MODE_CUSTOM
            obj[c4d.ID_BASELIST_ICON_COLOR] = color

            obj[c4d.ID_BASEOBJECT_USECOLOR] = c4d.ID_BASEOBJECT_USECOLOR_ALWAYS
            obj[c4d.ID_BASEOBJECT_COLOR] = color
    finally:
        doc.EndUndo()
        c4d.EventAdd()


if __name__ == "__main__":
    main()